﻿/**
 * 转换UTC日期
 * 转换后的格式: yyyy-MM-dd
 */
function formatToDate(val) {
    if (val != null) {
        var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
        //月份为0-11，所以+1，月份小于10时补个0
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    }
    return "";
}


/**
 * 获取当前日期
 * 格式: yyyy-MM-dd
 */
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var curDate = year + seperator1 + month + seperator1 + strDate;
    return curDate;
}

/**
 * 给指定日期添加指定的天数并返回新的日期
 * @param {any} datetime
 * @param {any} days
 */

function addDay(datetime, days) {
    var old_time = new Date(datetime.replace(/-/g, "/")); //替换字符，js不认2013-01-31,只认2013/01/31
    var fd = new Date(old_time.valueOf() + days * 24 * 60 * 60 * 1000); //日期加上指定的天数
    var new_time = fd.getFullYear() + "-";
    var month = fd.getMonth() + 1;
    if (month >= 10) {
        new_time += month + "-";
    } else {
        //在小于10的月份上补0
        new_time += "0" + month + "-";
    }
    if (fd.getDate() >= 10) {
        new_time += fd.getDate();
    } else {
        //在小于10的日期上补0
        new_time += "0" + fd.getDate();
    }
    return new_time; //输出格式：2013-01-02
}

